package org.example.com.controller;

import org.example.com.pojo.Result;
import org.example.com.pojo.ServiceConfig;
import org.example.com.service.ServiceConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/services")
public class ServiceConfigController {

    @Autowired
    private ServiceConfigService serviceConfigService;

    //根据服务类别获取该类别下所有服务
    @GetMapping("/type/{configType}")
    public Result getServiceConfigsByType(@PathVariable String configType) {
        List<ServiceConfig> configs = serviceConfigService.getServiceConfigsByType(configType);
        return Result.success(configs);
    }

    //获取所有已上架的服务
    @GetMapping("/active")
    public Result getActiveServiceConfigs() {
        List<ServiceConfig> configs = serviceConfigService.getActiveServiceConfigs();
        return Result.success(configs);
    }

    //根据服务id来获取对应服务
    @GetMapping("/{id}")
    public Result getServiceConfigById(@PathVariable Integer id) {
        ServiceConfig config = serviceConfigService.getServiceConfigById(id);
        if (config != null) {
            return Result.success(config);
        }
        return Result.error("配置不存在");
    }

    //创建服务配置方法，暂未使用
    @PostMapping
    public Result createServiceConfig(@RequestBody ServiceConfig serviceConfig) {
        boolean success = serviceConfigService.createServiceConfig(serviceConfig);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }

    //修改服务配置内容方法，暂未使用
    @PutMapping("/{id}")
    public Result updateServiceConfig(@PathVariable Integer id, @RequestBody ServiceConfig serviceConfig) {
        serviceConfig.setId(id);
        boolean success = serviceConfigService.updateServiceConfig(serviceConfig);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    //删除服务配置方法，后续有机会使用
    @DeleteMapping("/{id}")
    public Result deleteServiceConfig(@PathVariable Integer id) {
        boolean success = serviceConfigService.deleteServiceConfig(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    // 新增整合方法，一次返回所有数据给前端渲染
    @GetMapping("/info")
    public Result getAllServiceInfo() {
        // 创建一个Map来存储所有服务信息
        Map<String, Object> serviceInfo = new HashMap<>();

        // 获取VIP服务列表
        List<ServiceConfig> vipServiceList = serviceConfigService.getServiceConfigsByType("vip_service");
        serviceInfo.put("vipServiceList", vipServiceList);

        // 获取家庭服务列表
        List<ServiceConfig> homeServiceList = serviceConfigService.getServiceConfigsByType("home_service");
        serviceInfo.put("homeServiceList", homeServiceList);

        // 获取服务活动
        List<ServiceConfig> serviceActivity = serviceConfigService.getServiceConfigsByType("service_activity");
        serviceInfo.put("serviceActivity", serviceActivity);

        // 获取新手区域
        List<ServiceConfig> newcomerZone = serviceConfigService.getServiceConfigsByType("newcomer_zone");
        serviceInfo.put("newcomerZone", newcomerZone);

        // 获取清洁服务信息
        List<ServiceConfig> cleaningServiceInfo = serviceConfigService.getServiceConfigsByType("cleaning_service");
        serviceInfo.put("cleaningServiceInfo", cleaningServiceInfo);

        return Result.success(serviceInfo);
    }
}
